package com.wyvoid.forum.web.exception;

import com.wyvoid.forum.exception.admin.AdminException;
import com.wyvoid.forum.exception.user.UserNotLoginException;
import com.wyvoid.forum.exception.user.AuthorizationException;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.exception.AutoExceptionHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统异常处理
 * <p>51void.com</p>
 * User:cjp
 * Date: 2007-5-26
 * Time: 5:56:16
 */
public class SecurityExceptionHandler implements AutoExceptionHandler {
    Log log = LogFactory.getLog(SecurityExceptionHandler.class);

    public Resolution handleUserNotLoginException(UserNotLoginException e, HttpServletRequest req, HttpServletResponse res) {
        return new RedirectResolution("/user/login.do?loginForm");
    }

    public Resolution handleAdminException(AdminException e, HttpServletRequest req, HttpServletResponse res) {
        return new RedirectResolution("/admin/login.do?loginForm");
    }

    public Resolution handleAuthorizationException(AuthorizationException e,HttpServletRequest req, HttpServletResponse res){
        return new RedirectResolution("/user/unAuthority.do");
    }

    public Resolution handleOtherException(Throwable throwable, HttpServletRequest request, HttpServletResponse response) {
        log.error(throwable);
        throwable.printStackTrace();
        request.setAttribute("error", throwable);
        return new ForwardResolution("/error.jsp");
    }
}
